import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'
const baseUrl = CommonConfig.messagestationUrl + '/v1'
const upms = CommonConfig.upmsUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
 *  获取消息设置消息类别
 * /v1/message/business/list/label
 * */
function getBusinessListLabel () {
  const url = baseUrl + '/message/business/list/label'
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}
/**
 * 列举角色
 * @param [keyword] {string} 关键字
 * @returns {Promise<unknown>} promise
 */
function getlistRoles (keyword) {
  const url = upms + '/role_list'
  let config = {
    params: {
      name: keyword
    }
  }
  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 *
 *
 * /v1/message/template/list 查询
 *  returnContent 是否显示内容, 列表固定false  label  类别， groupId 接收角色  enable  状态
 */
function getMessagelist ({
  label,
  groupId,
  enable,
  pageNum,
  pageSize
}) {
  const url = baseUrl + '/message/template/list'
  const groupIds = groupId[0] === 'all' ? null : groupId.join()
  const params = {
    // returnContent: false,
    label: label === 'all' ? null : label,
    groupId: groupIds,
    enable: enable === 'all' ? null : enable,
    pageNum,
    pageSize
  }
  return axiosInstance.get(url, { params }).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 分页列举消息模板
 * @param params
 * @returns {Promise<unknown>}
 */
export function getMessageList (params) {
  const url = baseUrl + '/message/biz_templates:page'
  const config = {
    params,
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(url, config).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 分页列举消息模板
 * @param id
 * @param params
 * @returns {Promise<unknown>}
 */
export function putMessage (id, params) {
  const url = baseUrl + `/message/biz_templates/${id}`
  return axiosInstance.put(url, params).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 分页列举消息模板
 * @param id
 * @returns {Promise<unknown>}
 */
export function delMessage (id) {
  const url = baseUrl + `/message/biz_templates/rules/${id}`
  return axiosInstance.delete(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

// /message/business/list
// 获取消息概要 查询业务字典
function getBusinessList () {
  const url = baseUrl + '/message/business/list'
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

// 设置启用
// enableFlag  Boolean true启用 false 禁用
// id
function putMessageEnable ({ id, enableFlag }) {
  const url = baseUrl + `/message/template/enable/${id}?enableFlag=${enableFlag}`
  return axiosInstance.put(url).then((res) => {
    return Promise.resolve((res))
  })
}

// 获取示例详情
function getMessageDetail (id) {
  const url = baseUrl + `/message/template/detail/${id}`
  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 *
 *推送设置弹窗保存
  /v1/message/template/update PUT
  修改
  business 消息概要
  id
  rule 触发条件
  sendWay []
  system: 0 dlp 写死为0
 */
function putMessageUpdate ({
  business,
  id,
  rule,
  sendWay
}) {
  const url = baseUrl + '/message/template/update'
  const params = {
    business,
    id,
    rule,
    sendWay,
    system: 0
  }
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 *
 *选择角色时保存调用
  如果角色列表不为空 再调用 PUT
  /v1/message/group/bind/{userId}
  绑定分发id
  userId 当前用户id
  business
  groupIds  角色列表id
 */
function putMessageBind ({
  business,
  id,
  groupIds,
  userId
}) {
  const url = baseUrl + `/message/group/bind/${userId}`
  const params = {
    business,
    id,
    groupIds
  }
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}

export default {
  getlistRoles,
  getMessagelist,
  getBusinessList,
  getBusinessListLabel,
  putMessageEnable,
  getMessageDetail,
  putMessageUpdate,
  putMessageBind
}
